﻿@inherits ClientSectionCompontentBase

<MRow Class="pt-4">
    <MCol Md=12>
        <STextField @bind-Value="Dto.ClientId" Disabled Label="@T("ClientId")" />
    </MCol>
    <MCol Md=12>
        <STextField Required @bind-Value="Dto.ClientName" Label="@T("ClientName")" />
    </MCol>
    <MCol Md=12>
        <STextField @bind-Value="Dto.Description" Label="@T("Description")" />
    </MCol>
    <MCol Md=12 Class="py-0">
        <MCheckbox @bind-Value="Dto.RequireRequestObject" Label="@T("RequireRequestObject")" TValue="bool"></MCheckbox>
    </MCol>
    <MCol Md=12>
        <div class="block-center">
            <STextField @bind-Value="Dto.AllowedCorsOrigin" Label="@T("AllowedCorsOrigins")"/>
            <SButton Icon OnClick="AddAllowedCorsOrigin" Color="regular3">
                <SIcon>@IconConstants.Add</SIcon>
            </SButton>
        </div>
    </MCol>
    @foreach (var origin in Dto.AllowedCorsOrigins)
    {
        <MCol Md=6>
            <div class="block-center">
                <STextField HideDetails="true" Value="origin" Filled Disabled Class="flex-grow-1" />
                <SButton Icon OnClick="() => RemoveAllowedCorsOrigin(origin)" Color="regular3">
                    <SIcon Tooltip="@T("Delete")">mdi-close-circle</SIcon>
                </SButton>
            </div>
        </MCol>
    }
    <MCol Md=12>
        <h5 class="subtitle2 default--text mb-4">
            @T("Properties")
            <SIcon Small Tooltip="@T("ClientPropertiesTips")">mdi-help-circle-outline</SIcon>
        </h5>
        <MRow>
            <MCol>
                <STextField @bind-Value="@Dto.Property.Key" Label="@T("Key")" />
            </MCol>
            <MCol>
                <STextField @bind-Value="@Dto.Property.Value" Label="@T("Value")" />
            </MCol>
            <MCol Md="@("auto")" Class="d-flex align-center">
                <SButton Class="mr-4" Icon OnClick="AddProperty" Color="regular3">
                    <SIcon Size=32>@IconConstants.Add</SIcon>
                </SButton>
            </MCol>
            <MCol Md=12>
                <MDataTable Headers="_headers" Dense Items="Dto.Properties" TItem="ClientPropertyDto" HideDefaultFooter DisablePagination Class="margin-border">
                    <HeaderColContent Context="header">
                        <span class="body2">@header.Text</span>
                    </HeaderColContent>
                    <ItemColContent>
                        @switch (context.Header.Value)
                        {
                            case "Action":
                                <SButton Class="mr-4" Icon OnClick="()=>Dto.Properties.Remove(context.Item)" Color="regular">
                                    <SIcon Tooltip="@T("Delete")">mdi-close</SIcon>
                                </SButton>
                                break;
                            default:
                                <span class="subtitle">@context.Value</span>
                                break;
                        }
                    </ItemColContent>
                </MDataTable>
            </MCol>
        </MRow>
    </MCol>
</MRow>

@code {
    [Parameter]
    public ClientBasicDto Dto { get; set; } = new();

    [Parameter]
    public EventCallback<ClientBasicDto> DtoChanged { get; set; }

    List<DataTableHeader<ClientPropertyDto>> _headers = new();

    protected override void OnAfterRender(bool firstRender)
    {
        if (firstRender)
        {
            _headers = new()
            {
                new() { Text = T("Key"), Value = nameof(ClientPropertyDto.Key), Sortable = false },
                new() { Text = T("Value"), Value = nameof(ClientPropertyDto.Value), Sortable = false },
                new() { Text = T("Action"), Value = "Action", Sortable = false, Align = DataTableHeaderAlign.Center },
            };
        }
        base.OnAfterRender(firstRender);
    }

    private void RemoveAllowedCorsOrigin(string val)
    {
        Dto.AllowedCorsOrigins.Remove(val);
    }

    private void AddAllowedCorsOrigin()
    {
        if (!Uri.TryCreate(Dto.AllowedCorsOrigin, UriKind.Absolute, out var uri))
        {
            PopupService.EnqueueSnackbarAsync(T("Is not a valid URL address"), AlertTypes.Error);
            return;
        }
        if (string.IsNullOrWhiteSpace(Dto.AllowedCorsOrigin))
        {
            PopupService.EnqueueSnackbarAsync(T("Cannot be empty"), AlertTypes.Error);
            return;
        }
        if (Dto.AllowedCorsOrigins.Contains(Dto.AllowedCorsOrigin.Trim()))
        {
            PopupService.EnqueueSnackbarAsync(T("Duplicate item"), AlertTypes.Error);
            return;
        }
        Dto.AllowedCorsOrigins.Add(Dto.AllowedCorsOrigin);
        Dto.AllowedCorsOrigin = string.Empty;
    }

    private void AddProperty()
    {
        if (string.IsNullOrWhiteSpace(Dto.Property.Key) || string.IsNullOrWhiteSpace(Dto.Property.Value))
        {
            PopupService.EnqueueSnackbarAsync(T("Cannot be empty"), AlertTypes.Error);
            return;
        }
        if (Dto.Properties.Any(p => p.Key == Dto.Property.Key.Trim()))
        {
            PopupService.EnqueueSnackbarAsync(T("Duplicate key"), AlertTypes.Error);
            return;
        }
        Dto.Properties.Add(Dto.Property);
        Dto.Property = new();
    }
}
